#include"headfile.h"
#include "TFT.h"

//-------------------------------------------------------------------------------------------------------------------
//  @brief      屏幕头显示
//  @param      void
//  @return     void		
//  @note       TFT     
//-------------------------------------------------------------------------------------------------------------------
void Dis_Head(signed char String[])
{
    unsigned char Temp_Char = 0, Temp_j = 0, Position_x = 8;
    //unsigned char Temp_i=0;
	unsigned char Over_Flag = 0;
	unsigned char String_Length = 0;
	unsigned char Begin = 0;

    while(String[String_Length] != '\0')
        String_Length++;

    if(String_Length>HEAD_MAX_WORD)
        Begin=0;
    else 
        Begin=(signed char)((18-String_Length)>>1);
    
    while(Temp_j<HEAD_MAX_WORD)
    {
        if(Temp_j<Begin)
            lcd_showchar(Position_x,0,' ');
        else
        {
            Temp_Char=*(String++);
            if(Temp_Char=='\0')
                Over_Flag=1;
            if(!Over_Flag)
                lcd_showchar(Position_x,0,Temp_Char);
            else 
                lcd_showchar(Position_x,0,' ');

        }
        Position_x+=8;
        Temp_j++;
    }
}


/******************************************************/
/********************  功能函数    ********************/
/******************************************************/



//-------------------------------------------------------------------------------------------------------------------
//  @brief      *OLED写七位字符串*
//  @param      unsigned char y, unsigned char x, signed char String[]
//  @return     void		
//  @note       TFT     
//-------------------------------------------------------------------------------------------------------------------
void OLED_Write_String_7(unsigned char y, unsigned char x, signed char String[])
{
    unsigned char Temp_Char=0, Temp_j = 0;
   //uint8 Temp_K=0,Temp_i=0;
    unsigned char String_Length=0;

    while (String[String_Length] != '\0')
		String_Length++;
    if(String_Length>MAX_WORD)
        String_Length=MAX_WORD;
    
    while(Temp_j<MAX_WORD)
    {
        String_Length++;
        if(String_Length>MAX_WORD)
        {
            Temp_Char=*(String++);
            lcd_showchar(x,y*8,Temp_Char);
        }
        else 
            lcd_showchar(x,y*8,' ');

            x+=8;
            Temp_j++;
    }
}
//-------------------------------------------------------------------------------------------------------------------
//  @brief      *显示字符串*
//  @param      unsigned char position, signed char String[]
//  @return     void		
//  @note       TFT     
//-------------------------------------------------------------------------------------------------------------------
void Dis_String(unsigned char Position, signed char String[])
{
    switch (Position)
	{
	case 1:OLED_Write_String_7(2, 8, String); break;
	case 2:OLED_Write_String_7(2, SCREEN_DEPART, String); break;
	case 3:OLED_Write_String_7(4, 8, String); break;
	case 4:OLED_Write_String_7(4, SCREEN_DEPART, String); break;
	case 5:OLED_Write_String_7(6, 8, String); break;
	case 6:OLED_Write_String_7(6, SCREEN_DEPART, String); break;
	case 7:OLED_Write_String_7(8, 8, String); break;
	case 8:OLED_Write_String_7(8, SCREEN_DEPART, String); break;
	case 9:OLED_Write_String_7(10, 8, String); break;
	case 10:OLED_Write_String_7(10, SCREEN_DEPART, String); break;
	case 11:OLED_Write_String_7(12, 8, String); break;
	case 12:OLED_Write_String_7(12, SCREEN_DEPART, String); break;
	case 13:OLED_Write_String_7(14, 8, String); break;
	case 14:OLED_Write_String_7(14, SCREEN_DEPART, String); break;
    default:break;
    }
}

//-------------------------------------------------------------------------------------------------------------------
//  @brief      *显示浮点型*
//  @param      unsigned char Position, float Number
//  @return     void		
//  @note       TFT     
//-------------------------------------------------------------------------------------------------------------------
void Dis_Float(unsigned char Position, float Number)
{
    signed char Temp_Buf[17];
    (void)Float_To_String(Number,Temp_Buf);
    Dis_String(Position,Temp_Buf);
}

//-------------------------------------------------------------------------------------------------------------------
//  @brief      *整型转字符串*
//  @param      signed long Int_Num,signed char String[]
//  @return     signed int 返回数组长度		
//  @note       TFT     
//-------------------------------------------------------------------------------------------------------------------
signed int Int_To_String(signed long Int_Num,signed char String[])
{
    signed int Num_Length=0;
    signed char Negative_Flag=0;
    signed char *Point_p=String;
    signed char *Point_s=String;

    if(Int_Num<0)
    {
        Negative_Flag=1;
        Int_Num=0-Int_Num;
    }
    if(!Int_Num)
        String[Num_Length++]='0';
    while(Int_Num>0)
    {
        String[Num_Length++]=Int_Num%10+'0';
        Int_Num=Int_Num/10;
    }
    if(Negative_Flag)
        String[Num_Length++]='-';
    String[Num_Length]='\0';
    Point_p=String+Num_Length-1;    //将P指针指向字符串结束标志之前

    for(;Point_p-Point_s>0;Point_p--,Point_s++)//将字符串中存储的数调头   
    {
        *Point_s^=*Point_p;
        *Point_p^=*Point_s;   
        *Point_s^=*Point_p; 
    }
    return(Num_Length);
}

//-------------------------------------------------------------------------------------------------------------------
//  @brief      *浮点数转字符串*
//  @param      float Float_Num,signed char String[]
//  @return     signed int 返回数组长度		
//  @note       TFT     
//-------------------------------------------------------------------------------------------------------------------
signed int Float_To_String(float Float_Num,signed char String[])
{
    signed int Num_Length=0,Temp_i=0;
    signed char* Point_p=String;            //做换向时用的指针
    signed char* Point_s=String;
    signed char Negative_Flag=0;
    signed long Predot_Num,Afterdot_Num;
    float Afterdot_Float;

    if(Float_Num<0)
    {
        Negative_Flag=1;
        Float_Num=0-Float_Num;
    }

    Predot_Num=(signed long)Float_Num;    //将小数点之前的数变为整数
    Afterdot_Float=Float_Num-Predot_Num;  //单独取出小数点之后的数
    for(Temp_i=DOT_SIZE;Temp_i>0;Temp_i--)
    {
        Afterdot_Float=Afterdot_Float*10;
    }
    Afterdot_Num=(signed long)Afterdot_Float;

    Temp_i=DOT_SIZE;

    while (Afterdot_Num>0|Temp_i>0)
    {
        Temp_i--;
        String[Num_Length++]=Afterdot_Num%10+'0';
        Afterdot_Num/=10;
    }
    String[Num_Length++]='.';
    
    if(!Predot_Num)     //处理小数点前为0的情况
        String[Num_Length++]='0';

    while(Predot_Num>0)
    {
        String[Num_Length++]=Predot_Num%10+'0';
        Predot_Num/=10;
    }
    
    if(Negative_Flag)  
        String[Num_Length++]='-';
    String[Num_Length]='\0';  //加上字符串结束标志
    
    Point_p=String+Num_Length-1;    //将P指针指向字符串结束标志之前

    for(;Point_p-Point_s>0;Point_p--,Point_s++) //将字符串中存储的数调头
    {
        *Point_s^=*Point_p;
        *Point_p^=*Point_s;   
        *Point_s^=*Point_p;   
    }

    return(Num_Length);
}
//-------------------------------------------------------------------------------------------------------------------
//  @brief      *显示整形数*
//  @param      unsigned char Position, signed int Number
//  @return     void	
//  @note       TFT     
//-------------------------------------------------------------------------------------------------------------------
void Dis_Int(unsigned char Position, signed int Number)
{
    signed char Temp_Buf[17];
   (void)Int_To_String(Number,Temp_Buf);

	Dis_String(Position,Temp_Buf);
}

//-------------------------------------------------------------------------------------------------------------------
//  @brief      *OLED写一个整型数据*
//  @param      unsigned char x,unsigned char y,signed int Number
//  @return     void	
//  @note       TFT     
//-------------------------------------------------------------------------------------------------------------------
void OLED_Write_Int(unsigned char x,unsigned char y,signed int Number)
{
    signed char Temp_Buf[17];
    (void)Int_To_String(Number,Temp_Buf);
    lcd_showstr(x,y,Temp_Buf);

}
